/*
 * main.cpp
 * This file is part of Space Invaders
 *
 * Copyright (C) 2009 - marcelo
 *
 * Space Invaders is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Space Invaders is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Space Invaders; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#include <stdio.h> 
#include <stdlib.h>

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif


#include <math.h>
#include "ship.h"
#include "imagehandler.h"
#include <queue>

ship nave1(0, -0.85);
bullet bullet1(-1,-1);
GLuint textureID; 

void windowInit( int, char** );
void renderScene( void );
void keyboard(unsigned char key, int x, int y);
void special (int key, int x, int y);
void initRendering();

int main(int argc, char** argv){

  windowInit(argc, argv);
  return 0;
}

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	
	Image * pImage;
	char name[20] = "Images/ship2.bmp";
	pImage = new Image(name);
	textureID = pImage->loadTexture();
	delete pImage;
}

void windowInit(int argc, char** argv){

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(320,320);
	glutCreateWindow("Invaders Of Space");
	
	initRendering();
	
	glutDisplayFunc(renderScene);
	glutIdleFunc(renderScene);
	glutKeyboardFunc(keyboard);
	glutSpecialFunc (special);
	glutMainLoop();
  
}

void special (int key, int x, int y){

  if (key == 27) 
    exit(0);
  if ( key == GLUT_KEY_RIGHT )
    nave1.move_xy( 0.05, 0.0, 1, -1, 1, -1 );
  if ( key == GLUT_KEY_LEFT )
    nave1.move_xy( -0.05, 0.0, 1, -1, 1, -1 );
  if ( key == GLUT_KEY_UP )
    nave1.move_xy( 0.0, 0.0, 1, -1, 1, -1 );
  if ( key == GLUT_KEY_DOWN )
    nave1.move_xy( 0.0, -0.0, 1, -1, 1, -1 );

  printf( "%d\n", key );
  nave1.print_atributes();
}

void keyboard(unsigned char key, int x, int y) {

  if (key == 27) 
    exit(0);

  if ( key == 32 ){
    nave1.fire();
    bullet1.move_to_xy( nave1.x, nave1.y );
  }

  printf( "%d\n", key );
  nave1.print_atributes();

}

void renderScene() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);

	nave1.render();
	bullet1.render();
	bullet1.move_xy(0.0, 0.001, 1, -1, 1, -1 );  //Coloquei a posicao inicial da bala 
	glFlush();
	glutSwapBuffers();
}

